package cn.myspringboot.service.impl;

import cn.myspringboot.domain.OpenIdResult;
import cn.myspringboot.result.JSONResult;
import cn.myspringboot.service.WechatRegisterService;
import cn.myspringboot.template.WechatLoginTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class WechatRegisterServiceImpl implements WechatRegisterService {
    @Autowired
    private WechatLoginTemplate wechatLoginTemplate;
    @Override
    public OpenIdResult getOpenId(String code) {
        return wechatLoginTemplate.getOpenId(code);
    }

//    private static final String APPID = "wx51010358098347dd";
//    private static final String APPSECRET = "18b280bdc57a1df851b9fc1db082a432";
//    private static final String WECHATOPENIDURL = "https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code";
//    private static final String PHONEURL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=%s ";
//    private static final String TOKENURL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";

//    @Override
//    public JSONResult<OpenIdResult> getOpenId(String code) {
//        //1.获取到微信的OpenIdurl
//        String OpenIdUrl = String.format(WECHATOPENIDURL, APPID, APPSECRET, code);
//        HttpResponse execute = HttpRequest.get(OpenIdUrl).execute();
//        String body = execute.body();
//        OpenIdResult openIdResult = JSON.parseObject(body, OpenIdResult.class);
//        String phone = getWethatPhone(code);
//        openIdResult.setPhone(phone);
//        return JSONResult.success(openIdResult);
//    }
//
//    public JSONResult<OpenIdResult> getTokenAccess() {
//        //1.获取到微信的Tokenurl
//        String TokenUrl = String.format(TOKENURL, APPID, APPSECRET);
//        //执行并发送请求
//        HttpResponse execute = HttpRequest.get(TokenUrl).execute();
//        //得到结果
//        String body = execute.body();
//        //转换为JSON对象
//        OpenIdResult getTokenResult = JSON.parseObject(body, OpenIdResult.class);
//        return JSONResult.success(getTokenResult);
//    }
//
//    private String getWethatPhone(String code) {
//        String getTokenUrl = String.format(TOKENURL, APPID, APPSECRET);
//        HttpResponse execute = HttpRequest.get(getTokenUrl).execute();
//        String body = execute.body();
//        String accessToken = JSON.parseObject(body).get("access_token").toString();
//        String getPhoneUrl = String.format(PHONEURL,accessToken);
//        HttpResponse execute1 = HttpRequest.post(getPhoneUrl).execute();
//        String body1 = execute1.body();
//        JSONObject jsonObject = JSON.parseObject(body1);
//        if(jsonObject.get("errmsg").toString().equals("ok")){
//            return JSONObject.parseObject(
//                    jsonObject.get("phone_info").toString()
//            ).get("phoneNumber").toString();
//        }
//        return getRandomPhone();
//    }
//
//
//    private String getRandomPhone() {
//        Random random = new Random();
//        return String.valueOf(Math.abs(random.nextLong())).substring(0,11);
//    }


}
